package cn.zb.zbrhback.auth.controller;

import cn.zb.zbrhback.auth.annotation.ZBPermission;
import cn.zb.zbrhback.auth.service.ILoginService;
import cn.zb.zbrhback.basic.util.AjaxResult;
import cn.zb.zbrhback.basic.util.LoginMap;
import cn.zb.zbrhback.org.domain.Employee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/login")
@ZBPermission(name = "登录管理",desc = "登录管理")
@Api(value = "登录Api",tags = {"登录操作接口"})
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping
    @ApiOperation(value = "登录",notes = "不需要参数")
    @ZBPermission(name = "登录权限",desc = "登录权限")
    public AjaxResult login(@RequestBody Employee employee){
        try {
            Map<String,Object> map = loginService.login(employee);
            return AjaxResult.me().setData(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("登录失败"+e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录",notes = "不需要参数")
    @ZBPermission(name = "退出登录",desc = "退出登录")
    public AjaxResult logout(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            //删除保存的token
            LoginMap.map.remove(token);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("退出登录失败"+e.getMessage()).setSuccess(false);
        }
    }

}
